//
//  HMNetworkTools.swift
//  Swift网络请求封装
//
//  Created by Apple on 16/11/21.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit
import AFNetworking

//  网络请求方式
enum RequestType {
    //  GET请求
    case GET
    //  POST请求
    case POST
}

class HMNetworkTools: AFHTTPSessionManager {

    //  单例全局访问点
    static let sharedTools: HMNetworkTools = {
        let tools = HMNetworkTools()
        //  添加可接受的数据类型
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    
    //  网络请求通用的方法
    func request(type: RequestType, url: String, params: Any?, callback: @escaping (Any?, Error?)->Void) {
        
        if type == .GET {
            
            self.get(url, parameters: params, progress: nil, success: { (_, response) in
                callback(response, nil)
            }, failure: { (_, error) in
                callback(nil, error)
            })
            
        } else {
            self.post(url, parameters: params, progress: nil, success: { (_, response) in
                callback(response, nil)
            }, failure: { (_, error) in
                callback(nil, error)
            })
            
        }
        
        
    }
    
    
    
    
    
}

// MARK:    -发微博相关接口
extension HMNetworkTools {
    
    //  发送带图片的微博接口
    func upload(accessToken: String, status: String, image: UIImage, callBack: @escaping (Any?, Error?) -> Void) {
        let url = "https://upload.api.weibo.com/2/statuses/upload.json"
        let params = [
            "access_token": accessToken,
            "status": status
        ]
        let imageData = UIImageJPEGRepresentation(image, 0.5)!
        post(url, parameters: params, constructingBodyWith: { (formData)->() in
            formData.appendPart(withFileData: imageData, name: "pic", fileName: "test", mimeType: "image/jpeg")
        
        }, progress: nil, success: { (dataTask, reponse)-> Void in
            callBack(reponse, nil)
        }, failure: { (dataTask, error)-> Void in
            callBack(nil, error)
        })
    }
    //  发送带文字微博接口
    func update(accessToken: String, status: String, callBack: @escaping (Any?, Error?) -> Void) {
        let url = "https://api.weibo.com/2/statuses/update.json"
        let params = [
            "access_token": accessToken,
            "status": status
        ]
        request(type: .POST, url: url, params: params, callback: callBack)
        
    }
}



// MARK:    -首页相关数据
extension HMNetworkTools {
    //  获取微博首页数据
    func requestStatus(accessToken: String, maxId: Int64 = 0, sinceId: Int64 = 0, callBack: @escaping (Any?, Error?) -> Void) {
        //  定义url接口
        let url = "https://api.weibo.com/2/statuses/friends_timeline.json"
        //  定义参数
        let params: [String: Any] = [
            "access_token": accessToken,
            "max_id": maxId,
            "since_id": sinceId
        ]
        print(url + "?access_token=" + accessToken)
        request(type: .GET, url: url, params: params, callback: callBack)
        
    }
    
}

// MARK:    -OAuth登录相关接口
extension HMNetworkTools {

    //  根据accesstoken和uid获取用户信息
    func requestUserInfo(accessToken: String, uid: Int64, callBack: @escaping (Any?, Error?) -> Void) {
        //  定义url接口地址
        let url = "https://api.weibo.com/2/users/show.json"
        //  参数
        let params: [String: Any] = [
            "access_token": accessToken,
            "uid": uid
        ]
        request(type: .GET, url: url, params: params, callback: callBack)
    }
    
    //  获取accesstoken
    func requestAccessToken(code: String, callBack: @escaping (Any?, Error?) -> Void) {
        
        //  定义url接口地址
        let url = "https://api.weibo.com/oauth2/access_token"
        let params = [
            "client_id": AppKey,
            "client_secret": AppSecret,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": Redirect_Uri
        ]
        request(type: .POST, url: url, params: params, callback: callBack)
        
    }
    
    
}















